﻿using Halcon二次开发.Model.配置信息;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.View.配置界面
{
    public partial class ConfigForm : Form
    {
        string[] dris;
        Dictionary<string,string> chridDirs = new Dictionary<string,string>();  //任务文件集合
        static ConfigForm instance;
        public static ConfigForm GetSingle() {
            if (instance==null)
            {
                instance = new ConfigForm();
            }
            return instance;
        }
        private ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = Application.StartupPath;
            //加载工程到Combox
            dris = Directory.GetDirectories(@"Data\工程文件");
            NgrootPath.Items.Clear();    
            NGChinldPath.Items.Clear();
            textBox5.Text = @"Data\工程文件";
            textBox4.Text = @"Data\训练图像";
            for (int i = 0; i < dris.Length; i++) {
                string[] stateName = dris[i].Split(new char[] { '工', '程', '文', '件', '\\' }, StringSplitOptions.RemoveEmptyEntries);
                string name = stateName[stateName.Length - 1];
                NgrootPath.Items.Add(name);

                string[] strings = Directory.GetDirectories(dris[i]);
                foreach (string item in strings)
                {
                    chridDirs.Add(dris[i], item);
                }
            }

            if (NgrootPath.Items.Count > 0) { 
                NgrootPath.SelectedIndex = 0;
            }
            textBox3.Text = @"logs";
            textBox6.Text = PathConfig.GetSingle().DiskValue;
        }

        private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
        /// <summary>
        /// 选择事件触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Text = $"{dris[NgrootPath.SelectedIndex]}\\OKNG图片\\NG";
            NGChinldPath.Items.Clear();
            foreach (var item in chridDirs)
            {
                if (dris[NgrootPath.SelectedIndex]==item.Key)
                {
                    string[] str = item.Value.Split(new char[] {'\\' }, StringSplitOptions.RemoveEmptyEntries);
                    if (str.Length>0)
                    {
                        string name = str[str.Length - 1];
                        NGChinldPath.Items.Add(name);
                    }
                    
                    
                }
            }

            if (NGChinldPath.Items.Count > 0)
            {
                NGChinldPath.SelectedIndex = 0;
            }
            else {
                NGChinldPath.Text = "";
            }
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        public void OpenDir(string path)
        {
            // 检查路径是否存在
            if (System.IO.Directory.Exists(path))
            {
                // 启动文件资源管理器进程，并打开指定文件夹
                Process.Start(new ProcessStartInfo()
                {
                    FileName = "explorer.exe", // 文件资源管理器的可执行文件
                    Arguments = path,    // 文件夹路径作为参数
                    UseShellExecute = true,    // 指示是否使用操作系统的shell来启动进程
                    RedirectStandardOutput = false, // 通常不需要重定向输出
                    RedirectStandardError = false,  // 通常不需要重定向错误
                    CreateNoWindow = false         // 允许创建窗口（对于文件资源管理器是必要的）
                });
            }
            else {
                MessageBox.Show("路径错误!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenDir(textBox2.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenDir(textBox5.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenDir(textBox3.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenDir(textBox4.Text);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (int.TryParse(textBox6.Text, out int value))
            {
                PathConfig.GetSingle().DiskValue = textBox6.Text;
                File.WriteAllText(@"Data\配置文件\AppConfig.config", textBox6.Text);
            }
            else MessageBox.Show("请输入正确的数字!");
        }

        private void NGChinldPath_SelectedValueChanged(object sender, EventArgs e)
        {
            textBox2.Text = "Data\\工程文件\\"+NgrootPath.Text+"\\"+NGChinldPath.Text+"\\OKNG图片\\NG";
        }

        //打开启动文件
        private void button1_Click(object sender, EventArgs e)
        {
            OpenDir(textBox1.Text);
        }
    }
}
